package com.ph.elec.gwt.register.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.ph.elec.data.Volunteer;
import com.ph.elec.gwt.register.client.ui.FirstPagePanel;
import com.ph.elec.gwt.register.client.ui.SecondPagePanel;

public class VolunteerSignUp implements EntryPoint {

	public DeckPanel pnlDeck = new DeckPanel();
	FirstPagePanel pnlFirst = new FirstPagePanel(this);
	SecondPagePanel pnlSecond = new SecondPagePanel(this);

	public void onModuleLoad() {

		pnlDeck.add(pnlFirst);
		pnlDeck.add(pnlSecond);
		pnlDeck.showWidget(0);
		
		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		RootPanel panel = RootPanel.get("app");
		panel.add(pnlDeck);
	}

	public void RegisterVolunteer() {
		Volunteer volunteer = new Volunteer();
		
		volunteer.setKey(pnlFirst.pnlContact.txtEmail.getText().trim());
		volunteer.setLastName(pnlFirst.pnlPersonal.txtLastName.getText().trim());
		volunteer.setFirstName(pnlFirst.pnlPersonal.txtFirstName.getText().trim());
		volunteer.setMiddleName(pnlFirst.pnlPersonal.txtMiddleName.getText().trim());
		volunteer.setBirthDay(
				String.valueOf(Integer.parseInt(pnlFirst.pnlPersonal.txtBdayYear.getText().trim())) + "/" + 
				pnlFirst.pnlPersonal.lstBdayMonth.getSelectedIndex() + "/" + 
				String.valueOf(Integer.parseInt(pnlFirst.pnlPersonal.txtBdayDay.getText().trim())));
		volunteer.setAge(Integer.parseInt(pnlFirst.pnlPersonal.txtAge.getText().trim()));
		volunteer.setCivilStatus(pnlFirst.pnlPersonal.lstCivilStatus.getSelectedIndex());
		volunteer.setOccupation(pnlFirst.pnlPersonal.lstOccupation.getSelectedIndex());
		volunteer.setGender(pnlFirst.pnlPersonal.lstGender.getSelectedIndex());
		volunteer.setEducationalAttainment(pnlFirst.pnlPersonal.lstEducation.getSelectedIndex());
		volunteer.setEmail(pnlFirst.pnlContact.txtEmail.getText().trim());
		volunteer.setTelephone(pnlFirst.pnlContact.txtTelephone.getText().trim());
		volunteer.setMobile(pnlFirst.pnlContact.txtMobile.getText().trim());
		volunteer.setTelephone(pnlFirst.pnlPersonal.txtLastName.getText());
		if (pnlSecond.pnlVolunteerHelp.chkVTime.getValue() &&
				pnlSecond.pnlVolunteerHelp.chkVCash.getValue()) {
			volunteer.setUserVolunteer(Volunteer.VolunteerType.TIME_CASH);
		} else if (pnlSecond.pnlVolunteerHelp.chkVTime.getValue()) {
			volunteer.setUserVolunteer(Volunteer.VolunteerType.TIME);
		} else if (pnlSecond.pnlVolunteerHelp.chkVCash.getValue()) {
			volunteer.setUserVolunteer(Volunteer.VolunteerType.CASH);
		} else {
			volunteer.setUserVolunteer(Volunteer.VolunteerType.NONE);
		}
		volunteer.setVolunteerAssignment(0);
		volunteer.setITAssignment(0);
		volunteer.setVolunteerAmount(0);
		if (pnlSecond.pnlVolunteerHelp.rdoVolunteer.getValue()) {
			if (volunteer.getUserVolunteer().equals(Volunteer.VolunteerType.TIME) ||
					volunteer.getUserVolunteer().equals(Volunteer.VolunteerType.TIME_CASH)) {
				volunteer.setVolunteerAssignment(pnlSecond.pnlVolunteerHelp.lstVPart.getSelectedIndex());
				volunteer.setITAssignment(pnlSecond.pnlVolunteerHelp.lstVITPart.getSelectedIndex());
			} else if (volunteer.getUserVolunteer().equals(Volunteer.VolunteerType.CASH) ||
					volunteer.getUserVolunteer().equals(Volunteer.VolunteerType.TIME_CASH)) {
				volunteer.setVolunteerAmount(Integer.valueOf(pnlSecond.pnlVolunteerHelp.txtCashAmount.getText().trim()));
			}			
		}
		volunteer.setRegion(OptionLists.REGION_CODES.get(pnlSecond.pnlAddress.lstAdrRegion.getSelectedIndex() - 1));
		volunteer.setProvince(pnlSecond.pnlAddress.lstAdrProvince.getValue(pnlSecond.pnlAddress.lstAdrProvince.getSelectedIndex()));
		volunteer.setCity(pnlSecond.pnlAddress.lstAdrCity.getValue(pnlSecond.pnlAddress.lstAdrCity.getSelectedIndex()));
		if (pnlSecond.pnlAddress.lstAdrBarangay.getSelectedIndex() > 0) {
			volunteer.setBarangay(pnlSecond.pnlAddress.lstAdrBarangay.getValue(pnlSecond.pnlAddress.lstAdrBarangay.getSelectedIndex()));
		} else {
			volunteer.setBarangay("");
		}
		volunteer.setStreetAddress(pnlSecond.pnlAddress.txtStreetAddr.getText().trim());
		volunteer.setZipcode(pnlSecond.pnlAddress.txtZipCode.getText().trim());
		volunteer.setDiocese(pnlSecond.pnlAddress.lstDiocese.getSelectedIndex());
		volunteer.setParish(pnlSecond.pnlAddress.txtParish.getText().trim());
		volunteer.setStreetAddress(pnlSecond.pnlAddress.txtStreetAddr.getText().trim());

		RegisterServiceAsync registerSv = GWT.create(RegisterService.class);
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
			public void onFailure(Throwable caught) {
			}
			public void onSuccess(Boolean result) {
				Window.open("/page/register/success.htm", "_self", null); 
			}
		};
		registerSv.RegisterVolunteer(volunteer, callback);
	}
}
